package com.example.administrator.chesstest01;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChessRoomActivity extends BaseActivity {

    @BindView(R.id.myView_wuziqi) MyView viewWuziqi;
    @BindView(R.id.btn_ready) Button btnReady;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case Common.JsonCmdResp.READY:
                    try {
                        JSONObject jo = new JSONObject(msg.obj.toString());
                        if (jo.getBoolean("is_opponent_ready")){
                            viewWuziqi.start(jo.getBoolean("is_white_chess"));
                        }
                        else{Toast.makeText(ChessRoomActivity.this, "请等待对手做好准备", Toast.LENGTH_SHORT).show();}
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case Common.JsonCmdReq.RECEIVE_CHESS:
                    viewWuziqi.setData(msg.obj.toString());
                    break;
                case Common.JsonCmdResp.NEW_PLAYER_JOIN_ROOM:
                    Toast.makeText(ChessRoomActivity.this, "有人来了", Toast.LENGTH_SHORT).show();
                    break;
                case Common.JsonCmdResp.OPPO_PLAYER_EXIT_ROOM:
                    Toast.makeText(ChessRoomActivity.this, "对手离开了", Toast.LENGTH_SHORT).show();
                default:
                    break;
            }
        }
    };
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //获得service中binder对象
            binder = (MyService.OptBinder) service;
            binder.setHandler(handler);
            viewWuziqi.setBinder(binder);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            binder = null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chess_room);
        Intent intent = new Intent(this,MyService.class);
        bindService(intent,conn, 0);
        ButterKnife.bind(this);
        Intent intent1 = getIntent();
        room_id = intent1.getIntExtra("room_id",0);
        System.out.println("-----------"+room_id);
        btnReady.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnalysisJson.Ready("123",binder);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AnalysisJson.SendExitRoom("123",room_id,binder);
        //activity消亡解除绑定
        if (null != conn){
            unbindService(conn);
        }
    }
}
